<?php
namespace app\index\controller;
use think\Controller;
use think\Db;	
use think\Session;
use think\request;

//修改
class Modifypwd extends Controller{
	//修改-
	public function index(){
		return $this->fetch('Modifypwd/index');
	}

	//修改
	public function usersave(){
	    $param = myarr($this->request->post());
//	    dump($param);die;
		$mobile = $param['mobile'];
		$pwd = $param['pwd'];
		$pwds = $param['pwds'];
		$code = $param['code'];
		$usercode = $this->codeVerification($mobile,$code);
		if(preg_match('/(^1[3|4|5|6|7|8|9]\d{9}$)|(^09\d{8}$)/', $mobile)!= 1){
			$this->error('手机号格式错误','Userfinds/index',1);
		}
		if(preg_match('/^[0-9a-zA-Z]+$/',$pwd) != 1){
			$this->error('密码格式错误','Userfinds/index');
		}
		if(preg_match('/^[0-9a-zA-Z]+$/',$pwds) != 1){
			$this->error('密码格式错误','Userfinds/index');
		}
		if(count($pwd) < 6 && count($pwd) > 15 ){
			$this->error('密码格式错误','Userfinds/index');
		}

		if($usercode['msg'] != 'success'){
			return $usercode;			
		}
		if($pwd!=$pwds){
			return json_encode(['msg'=>'密码不一致']);
		}
		
		$name = Db::name('users')->where(['phonenumber'=>createCode($mobile)])->field('phonenumber,id')->find();
		if($name==''){
			return json_encode(['msg'=>'此手机号暂未注册手机号'],JSON_UNESCAPED_UNICODE);
		}else{
//		// if($_POST['yaoqingma']!=null){
//			$pwd  = $_POST['pwd'];
//			$pwds = $_POST['pwds'];
			// $code = $_POST['code'];
			if($pwd==$pwds){
				$user=Db::name('users')->where(['id'=>$name['id']])->update(['password' => $pwd]);
				Db::name('user_code')->where(['mobile'=>createCode($mobile)])->order('id','desc')->update(['state'=>1]);
 				if($user){
 					$this->success('修改密码成功','Login/index');
 				}else{
 					$this->error('修改失败','Login/index');
 				}
			}			
		}
	}
	//验证用户是否注册
    
	//验证验证码
	public function codeVerification($mobile,$code){
        $param = $this->request->post();
		$mobile = $param['mobile'];
		$code = $param['code'];
//        机密手机号
        $jmmobile = createCode($mobile);
		if($mobile == null ){
			return ['msg'=>'手机号不能为空'];
		}
		if($code==null){
			return ['msg'=>'请输入验证码'];
		}else{
			$codes = Db::name('user_code')->where('mobile',$jmmobile)->order('id desc')->find();
			if($codes['code']==$code){
				if(time()>=$codes['reg_time']+1800){
					return ['msg'=>'验证码超时'];
				}else{
					return ['msg'=>'success'];
				}
			}else{
				return ['msg'=>'验证码错误'];
			}			
		}

	}

	// public function duanxin(){
	// 	$message = sendMsg(15757113254,'SMS_134815253','xiayulahaha');
	// 	dump($message);
	// }

	//注册获取短信验证码
	public function registersendMsg(){
	    $param = $this->request->post();
		$mobile = $param['mobile'];
		//注册手机号是否已经注册
//        机密手机号
        $jmmobile = createCode($mobile);
		$user = Db::name('users')->where(['phonenumber'=>$jmmobile])->find();
		if($user==''){
			return ['msg'=>'手机号未注册过账号'];
		}
		//一分钟只能发生一次
		$usercode = Db::name('user_code')->where('mobile',$jmmobile)->order('id desc')->find();
		if($usercode['reg_time']+60 >= time()){
			return ['msg'=>'一分钟只能发送一次'];
		}
		$tplCode = 'SMS_134815252';
		$tplParam = rand(1000,9999);
		//短信发送
		$message = sendMsg($mobile,$tplCode,$tplParam);
		//$message = ['Message'=>'OK','Code'=>'OK'];
		if($message['Message']=='OK'){
			$data = [
				'mobile' => $jmmobile,
				'code'   => $tplParam,
				'reg_time'=> time(),
			];
			$code = Db::name('user_code')->insert($data);
			if($code){
				return ['msg'=>'success'];
			}else{
				return ['msg'=>'验证码发送错误'];
			}
		}else{
			return ['msg'=>'验证码发送错误'];
		}

	} 			
}
?>